package com.boxin.ims.modules.wechat.busi;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.service.WechatMemberService;
import com.boxin.ims.modules.wechat.utils.ScoreUtils;

@Service
public class DeleteScoreProcessor extends BusinessProcessor {

	@Autowired
	private WechatMemberService wechatMemberService ;
	
	@Override
	public ProcessMessage process() {
		WechatMember member = memberCheckAndReply();
		if(member!=null){
			
			String keyword = getTextMessage().getContent();
			
			keyword = keyword.replaceAll("消除", "").trim();
			String regex ="^\\d+$";
			Pattern pattern = Pattern.compile(regex);
			Matcher matcher =  pattern.matcher(keyword);
			if(matcher.matches()){
				String delScore = matcher.group();
				String reply ="对不起,未能找到代号为["+delScore+"]的奖品,请确认是否输入正确,谢谢!";
				try {
					int dScore = Integer.parseInt(delScore)*1000;
					if(member.getActiveScore()> dScore){
						reply = "已成功消除了"+delScore+"个积分,您现在的活动积分为:"+((double)(member.getActiveScore()-dScore)/1000)+",谢谢!";
						ScoreUtils.addScore(member, -dScore,"0","手动消除积分");
						//member.setActiveScore(member.getActiveScore()-dScore);
						//wechatMemberService.save(member);
						writeToUser(reply);
						return null;
					}else{
						reply = "对不起,消除失败,您的活动积分为"+((double)member.getActiveScore()/1000)+"个积分,不够消除:"+((double)dScore/1000)+"个积分,谢谢!";
						writeToUser(reply);
						return null;
					}
					
				} catch (Exception e) {
					e.printStackTrace();
					reply = "对不起系统处理异常,请稍后再试,谢谢!";
					writeToUser(reply);
					return null;
				}
			
			}else{
				String reply = "亲，您输入的格式不正确,请输入如:【消除10】,谢谢!";
				writeToUser(reply);
			}
		}
			
		return null;
	}

	@Override
	public boolean isNeedBeMember() {
		return false;
	}
	
	

}
